package com.gulimall.product.web;

import com.gulimall.product.entity.CategoryEntity;
import com.gulimall.product.service.CategoryService;
import com.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author mayifan
 * @version 1.0
 * @date 2024-12-04 12:54
 */

@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){

        List<CategoryEntity> categorys =  categoryService.getLevel1Categorys();

        model.addAttribute("categorys",categorys);
        return "index";
    }

    @GetMapping("/index/catalog.json")
    @ResponseBody
    public Map<String, List<Catelog2Vo>> getCatelogJson(){

        Map<String, List<Catelog2Vo>> map = categoryService.getCatelogJson();
        return map;
    }

    @GetMapping("/hello")
    @ResponseBody
    public String hello(){
        return "Hello";
    }


    @GetMapping("/write")
    @ResponseBody
    public String write(){
        RReadWriteLock writeRead = redissonClient.getReadWriteLock("lock-wr");
        String s = "";
        RLock rLock = writeRead.writeLock();
        try {
            rLock.lock();
            s = UUID.randomUUID().toString();
            Thread.sleep(3000);
            stringRedisTemplate.opsForValue().set("writeRead",s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;
    }

    @GetMapping("/read")
    @ResponseBody
    public String read(){
        RReadWriteLock writeRead = redissonClient.getReadWriteLock("lock-wr");
        String s = "";
        RLock rLock = writeRead.readLock();
        try {
            rLock.lock();
            Thread.sleep(3000);
            s = stringRedisTemplate.opsForValue().get("writeRead");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;

    }

    /**
     * 放假，锁门
     * 5个班全部走完，我们可以锁大门
     */
    @GetMapping("/lock")
    @ResponseBody
    public String lockdoor() throws InterruptedException {
        RCountDownLatch countDownLatch = redissonClient.getCountDownLatch("door");
        countDownLatch.trySetCount(5);
        countDownLatch.await();
        return "放学了。。。";
    }

    @GetMapping("/gogogo/{id}")
    @ResponseBody
    public String go(@PathVariable Long id){
        RCountDownLatch countDownLatch = redissonClient.getCountDownLatch("door");
        countDownLatch.countDown();
        return id+"走了";
    }

    /**
     * 车库停车
     * 3停车位
     *
     *
     */
    @GetMapping("/park")
    @ResponseBody
    public String park() throws InterruptedException {
        RSemaphore park = redissonClient.getSemaphore("park");

        //占一个车位
//        park.acquire();
        //不会阻塞式等待
        boolean b = park.tryAcquire();
        return new String(String.valueOf(b));
    }

    @GetMapping("/go")
    @ResponseBody
    public String go(){
        RSemaphore park = redissonClient.getSemaphore("park");
        //释放一个车位
        park.release();
        return "ok";
    }


}
